<?php
/**
 * Created by PhpStorm.
 * User: Dave
 * Date: 2018/4/20
 * Time: 11:56
 */

namespace Biz\service\trade;

use Biz\model\trade\Order as mOrder;
use Biz\model\trade\OrderPay as mOrderPay;
use Biz\module\Classes\mgt\OrderPayExcel as mgtOrderPayExcel;
/**
 * @deprecated
 */
class OrderPay
{
    public static function getOrderPayList($field, $where = [], $order = [], $limit = [])
    {

        $orderPayWhere = self::getOrderPayWhere($where);
        $orderPayList = mOrderPay::getOrderPayList($field, $orderPayWhere, $order, $limit);

        $orderPayList = array_map(function ($v) {
            $v['order_no'] = mOrder::getOrderNoList($v['order_no']);
            return $v;
        }, $orderPayList);

        $result = [
            'orderPayList' => $orderPayList,
            'orderPaycount' => mOrderPay::getOrderPayCount($orderPayWhere) ?: 0,
            'OrderPayAmount' => mOrderPay::getOrderPayAmount($orderPayWhere) ?: 0,
            'statusList' => mOrderPay::PAY_STATUS_DESC,
            'payWay' => mOrderPay::_PAY_WAYS_DESC,
        ];
        return $result;
    }

    public static function mgtDownList($field, $data)
    {

        $orderPayWhere = self::getOrderPayWhere($data);

        mgtOrderPayExcel::downList($field, $orderPayWhere);
    }

    public static function getOrderPayWhere($where)
    {
        if ($where['payWay'] != '-1') {
            if (in_array($where['payWay'], mOrderPay::_WECHAT_PAY_WAYS_LIST)) {
                $orderPayWhere['payWay'] = mOrderPay::_WECHAT_PAY_WAYS_LIST;
            } else if (in_array($where['payWay'], mOrderPay::_ALIPAY_PAY_WAYS_LIST)) {
                $orderPayWhere['payWay'] = mOrderPay::_ALIPAY_PAY_WAYS_LIST;
            } else {
                $orderPayWhere['payWay'] = $where['payWay'];
            }
        }

        if ($where['start_time']) {
            $orderPayWhere['start_time'] = $where['start_time'];
        }

        if ($where['end_time']) {
            $orderPayWhere['end_time'] = $where['end_time'];
        }

        if ($where['orderNo']) {
            $orderPayWhere['orderNo'] = $where['orderNo'];
            $data = explode('-', $where['orderNo']);
            $orderPayWhere['orderPno'] = $where['orderNo'];
            if (is_numeric($data['3'])) {
                $data['3'] = 'P';
                $orderPayWhere['orderPno'] = implode('-', $data);

            }
        }

        if ($where['status'] != '-2') {
            $orderPayWhere['status'] = $where['status'];
        }

        if ($where['payNo']) {
            $orderPayWhere['payNo'] = $where['payNo'];
        }

        return $orderPayWhere;
    }
}